package com.itheima.person.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect//切面
@Component
public class Aspect1 {

    private static final Logger log = LoggerFactory.getLogger(Aspect1.class);

    // 增强方法逻辑
    @Around("execution(* com.itheima.person.service.impl.PersonServiceImpl.save(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        long start = System.nanoTime();

        log.debug(pjp.getSignature() + "开始执行");

        // pjp.proceed() 即表示调用目标方法（核心方法|被增强方法），或下一个通知
        Object result = pjp.proceed();

        long end = System.nanoTime();

        log.debug(pjp.getSignature() + "完毕执行");
        log.debug(pjp.getSignature() + "执行耗时: {} nm", (end - start));
        return result;
    }
    @Before("execution(* com.itheima.person.service.impl.PersonServiceImpl.update(..))")
    public void beforeCall(JoinPoint jp) {
        log.debug(jp.getSignature() + "方法，开始执行");
    }

    @After("execution(* com.itheima.person.service.impl.PersonServiceImpl.delete(..))")
    public void afterCall(JoinPoint jp) {
        log.debug(jp.getSignature() + "方法，执行结束,释放资源");
    }

    @AfterReturning(returning = "result",value = "execution(* com.itheima.person.service.impl.PersonServiceImpl.findAll(..))")
    public void afterReturningCall(JoinPoint jp, Object result) {
        log.debug(jp.getSignature() + "方法，执行结束,返回值:"+result);
    }
    @AfterThrowing(throwing = "ex",value = "execution(* com.itheima.person.service.impl.PersonServiceImpl.findById(..))")
    public void afterThrowingCall(JoinPoint jp, Throwable ex) {
        log.error(jp.getSignature() + "方法，执行发生异常" + ex.getClass().getName(), ex);
    }
}